package cn.zjh.org.controller;

import cn.zjh.basic.util.AjaxResult;
import cn.zjh.basic.util.PageList;
import cn.zjh.org.domain.Employee;
import cn.zjh.org.query.EmployeeQuery;
import cn.zjh.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口类
 */
@Api(value = "员工接口类", description = "员工crud的相关操作")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    //service注入
    @Autowired
    private IEmployeeService employeeService;

    //查找单个内容
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.findOne(id);
    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    //添加或者修改
    @PutMapping
    @ApiOperation(value = "添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new AjaxResult(false, "操作成功");
        }
    }

    //分页查询
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Employee> queryPage(EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }


}

